package com.wyix.controller;

import com.wyix.model.User;
import com.wyix.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @author wangdx
 * @since 2025/8/8
 */
@Controller
@RequestMapping("/users")
public class UserController {

    private final UserService userService;

    @Autowired // 构造函数注入
    public UserController(UserService userService) {
        this.userService = userService;
    }

    // 显示所有用户
    @GetMapping
    public String listUsers(Model model) {
        model.addAttribute("users", userService.getAllUsers());
        model.addAttribute("title", "用户列表");
        return "users/list";
    }

    // 显示单个用户详情
    @GetMapping("/{id}")
    public String viewUser(@PathVariable Integer id, Model model) {
        User user = userService.getUserById(id);
        if (user == null) {
            model.addAttribute("error", "用户不存在");
            return "error";
        }
        model.addAttribute("user", user);
        model.addAttribute("title", "用户详情");
        return "users/view";
    }

    // 显示创建用户表单
    @GetMapping("/new")
    public String showCreateForm(Model model) {
        model.addAttribute("user", new User());
        model.addAttribute("title", "创建新用户");
        return "users/form";
    }

    // 处理创建用户请求
    @PostMapping
    public String createUser(@ModelAttribute User user, RedirectAttributes redirectAttributes) {
        User createdUser = userService.createUser(user);
        redirectAttributes.addFlashAttribute("message", "用户 " + createdUser.getUsername() + " 创建成功！");
        return "redirect:/users";
    }

    // 显示编辑用户表单
    @GetMapping("/{id}/edit")
    public String showEditForm(@PathVariable Integer id, Model model) {
        User user = userService.getUserById(id);
        if (user == null) {
            model.addAttribute("error", "用户不存在");
            return "error";
        }
        model.addAttribute("user", user);
        model.addAttribute("title", "编辑用户");
        return "users/form";
    }

    // 处理更新用户请求
    @PostMapping("/{id}")
    public String updateUser(@PathVariable Integer id, @ModelAttribute User user, RedirectAttributes redirectAttributes) {
        user.setId(id); // 确保ID一致
        User updatedUser = userService.updateUser(user);
        if (updatedUser == null) {
            redirectAttributes.addFlashAttribute("error", "更新用户失败，用户不存在");
        } else {
            redirectAttributes.addFlashAttribute("message", "用户 " + updatedUser.getUsername() + " 更新成功！");
        }
        return "redirect:/users";
    }

    // 处理删除用户请求
    @GetMapping("/{id}/delete")
    public String deleteUser(@PathVariable Integer id, RedirectAttributes redirectAttributes) {
        User user = userService.getUserById(id);
        if (user != null) {
            userService.deleteUser(id);
            redirectAttributes.addFlashAttribute("message", "用户 " + user.getUsername() + " 删除成功！");
        } else {
            redirectAttributes.addFlashAttribute("error", "删除用户失败，用户不存在");
        }
        return "redirect:/users";
    }
}
